package org.jmc.refmanager.web.wicket.model;

import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.jmc.refmanager.domain.model.Role;
import org.jmc.refmanager.domain.service.RoleManager;

public class RoleModel implements IModel<Role> {
	private static final long serialVersionUID = 1L;

	private Integer id;
	private transient Role role;
	
	@SpringBean(name="roleManager")
	private RoleManager roleManager;
	
	
	/**
	 * loadUserByUsername
	 */
	public RoleModel(Role role) {
		super();
		InjectorHolder.getInjector().inject(this);
		this.id = role.getId();
		this.role = role;
	}

	@Override
	public void detach() {
		this.role = null;
	}

	@Override
	public Role getObject() {
		if (this.role == null && this.id != null ) {
			this.role = roleManager.findById(this.id);
		}
		
		return this.role;
	}

	@Override
	public void setObject(Role role) {
		throw new UnsupportedOperationException("Model " 
				+ getClass() + " does not support setObject(Object)");
	}

}
